package my.dao.impl;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import my.dao.Dao;
import my.dao.DaoException;

public class DaoConfig {
	
	/**
	 * 延迟加载配置
	 */
	private static final int MODE_LAZY=0;
	
	/**
	 * 立即加载配置
	 */
	private static final int MODE_FETCH=1;
	
	private static class EmptyDaoConfigInner{
		private static DaoConfig config=new DaoConfig(MODE_LAZY);
	}
	
	private static class FullDaoConfigInner{
		private static DaoConfig config=new DaoConfig(MODE_FETCH);
	}
	
	public static DaoConfig getLazyInstance(){
		return EmptyDaoConfigInner.config;
	}
	
	public static DaoConfig getInstance(){
		return FullDaoConfigInner.config;
	}
	
	private String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private String url = "jdbc:sqlserver://localhost:1433;databaseName=mydb";
	private String user = "sa";
	private String password = "11fit";
	private boolean use_transaction = false;

	public DaoConfig() {
		this(MODE_LAZY);
	}
	
	private DaoConfig(int mode){
		if(mode==MODE_LAZY)return;
		try{
			Properties props=new Properties();
			InputStream is=TransactionDaoImpl.class.getResourceAsStream(Dao.CONNECTION_SOURCE);
			if(is==null){
				throw new DaoException("在类的根路径下没有找到配置文件db.properties !");
			}
			props.load(is);
			
			if(props.getProperty(Dao.KEY_DRIVER_CLASS)!=null){
				driver_class=props.getProperty(Dao.KEY_DRIVER_CLASS);
			}else{
				throw new DaoException("在Dao配置文件中找不到属性："+Dao.KEY_DRIVER_CLASS);
			}
			if(props.getProperty(Dao.KEY_URL)!=null){
				url=props.getProperty(Dao.KEY_URL);
			}else{
				throw new DaoException("在Dao配置文件中找不到属性："+Dao.KEY_URL);
			}
			if(props.getProperty(Dao.KEY_USERNAME)!=null){
				user=props.getProperty(Dao.KEY_USERNAME);
			}else{
				throw new DaoException("在Dao配置文件中找不到属性："+Dao.KEY_USERNAME);
			}
			if(props.getProperty(Dao.KEY_PASSWORD)!=null){
				password=props.getProperty(Dao.KEY_PASSWORD);
			}else{
				throw new DaoException("在Dao配置文件中找不到属性："+Dao.KEY_PASSWORD);
			}
			
			if(props.getProperty(Dao.KEY_TRANSACTION_USE)!=null){
				try {
					use_transaction=Boolean.parseBoolean(props.getProperty(Dao.KEY_TRANSACTION_USE));
				} catch (Exception e) {
					System.err.println("警告：在Dao配置文件中，发现属性"+Dao.KEY_TRANSACTION_USE+"配置错误！属性"+Dao.KEY_TRANSACTION_USE+"默认取值false 。");
				}
			}else{
				System.err.println("警告：在Dao配置文件中未配置属性："+Dao.KEY_TRANSACTION_USE+"！属性"+Dao.KEY_TRANSACTION_USE+"默认取值false 。");
			}
		
			Class.forName(driver_class);
			
		} catch (Exception e) {			
			e.printStackTrace();
			throw new DaoException("Dao初始化异常："+e);
		}
	}
	
	public void init(Properties props){
		try {
			if(props.getProperty(Dao.KEY_DRIVER_CLASS)!=null){
				driver_class=props.getProperty(Dao.KEY_DRIVER_CLASS);
			}else{
				throw new DaoException("在Dao配置中找不到属性："+Dao.KEY_DRIVER_CLASS);
			}
			if(props.getProperty(Dao.KEY_URL)!=null){
				url=props.getProperty(Dao.KEY_URL);
			}else{
				throw new DaoException("在Dao配置中找不到属性："+Dao.KEY_URL);
			}
			if(props.getProperty(Dao.KEY_USERNAME)!=null){
				user=props.getProperty(Dao.KEY_USERNAME);
			}else{
				throw new DaoException("在Dao配置中找不到属性："+Dao.KEY_USERNAME);
			}
			if(props.getProperty(Dao.KEY_PASSWORD)!=null){
				password=props.getProperty(Dao.KEY_PASSWORD);
			}else{
				throw new DaoException("在Dao配置中找不到属性："+Dao.KEY_PASSWORD);
			}
			
			if(props.getProperty(Dao.KEY_TRANSACTION_USE)!=null){
				try {
					use_transaction=Boolean.parseBoolean(props.getProperty(Dao.KEY_TRANSACTION_USE));
				} catch (Exception e) {
					System.err.println("警告：在Dao配置中，发现属性"+Dao.KEY_TRANSACTION_USE+"配置错误！属性"+Dao.KEY_TRANSACTION_USE+"默认取值false 。");
				}
			}else{
				System.err.println("警告：在Dao配置中未配置属性："+Dao.KEY_TRANSACTION_USE+"！属性"+Dao.KEY_TRANSACTION_USE+"默认取值false 。");
			}
			
			

			Class.forName(driver_class);
			
		} catch (Exception e) {
			throw new DaoException("Dao初始化异常："+e);
		}
	}
	
	public void setDriver_class(String driver_class) {
		this.driver_class = driver_class;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUse_transaction(boolean use_transaction) {
		this.use_transaction = use_transaction;
	}

	public boolean useTransaction(){
		return use_transaction;
	}
	
	private DataSource dataSource;
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	Connection getConnection() throws SQLException {
		if(this.dataSource != null) {
			return dataSource.getConnection();
		}
		return DriverManager.getConnection(url,user,password);		
	}
	
	
	
	
}
